package com.team.six.bluecatshop.controller;

import com.team.six.bluecatshop.domain.Product;
import com.team.six.bluecatshop.domain.ProductType;
import com.team.six.bluecatshop.domain.SeachProductResponse;
import com.team.six.bluecatshop.service.ProductService;
import com.team.six.bluecatshop.service.ProductTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/adminProduct")
public class AdminProductController {
    @Autowired
    ProductService productService;
    @Autowired
    ProductTypeService productTypeService;

    /**
     * 跳转至商品展示页面
     */
    @GetMapping("/productList")
    public String productGetList(){
        return "administration/product_list";
    }

    /**
     * 处理商品展示请求
     */
    @GetMapping("/api/productList")
    @ResponseBody
    public SeachProductResponse seachProductResponse(Integer pageNum,Integer pageSize,String keyword){
        return productService.findProductAll(pageNum,pageSize,keyword);
    }

    /**
     * 跳转至新增商品请求页面并展示商品类型
     */
    @GetMapping("/addProductShow")
    public String addProductShow(Model model){
        List<ProductType> productTypes = productTypeService.findAll();
        model.addAttribute("productTypes",productTypes);
        return "administration/product-add";
    }

    /**
     * 新增商品请求
     */
    @PostMapping("/addProduct")
    public String addProduct(Product product){
        System.out.println(product.getPtypeId());
        if (product.getProductId().equals("")){
            productService.add(product);
        }else {
            productService.updateByPrimaryKey(product);
        }

        return "redirect:/adminProduct/productList";
    }

    /**
     * 处理删除请求
     */
    @GetMapping("/deleteProduct")
    public String deleteProduct(String productId){
        productService.remove(productId);
        return "redirect:/adminProduct/productList";
    }

    /**
     * 跳转至修改商品页面并展示商品类型
     */
    @GetMapping("/editProductShow")
    public String editProductShow(Product productEdit,Model model,String productTypeName){
        productEdit.setProductTypeName(productTypeName);
        model.addAttribute("productEdit",productEdit);
        List<ProductType> productTypes = productTypeService.findAll();
        model.addAttribute("productTypes",productTypes);
        return "administration/product-add";
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDelete")
    @ResponseBody
    public String batchDelete(String products){
        String[] split = products.split(",");
        for (int i=0;i<split.length;i++){
            //System.out.println(split[i]);
            productService.remove(split[i]);
        }
        System.out.println(products);
        return "删除成功";
    }
}
